package com.xp.kettlegenerate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xp.kettlegenerate.entity.DataBaseMeta;
import com.xp.kettlegenerate.mapper.DataBaseMetaMapper;
import com.xp.kettlegenerate.service.IDataBaseMetaService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xp.kettlegenerate.start.Generate;
import org.pentaho.di.core.database.DatabaseMeta;
import org.pentaho.di.core.exception.KettleXMLException;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xxx
 * @since 2024-06-17
 */
@Service
public class DataBaseMetaServiceImpl extends ServiceImpl<DataBaseMetaMapper, DataBaseMeta> implements IDataBaseMetaService {

    /**
     * 根据传入的数据库类型查找对应的数据库信息
     *
     * @param databaseType
     * @return
     */
    @Override
    public DatabaseMeta getDatabaseMeat(Integer databaseType) throws KettleXMLException {
        DataBaseMeta db = getOne(new LambdaQueryWrapper<DataBaseMeta>()
                .eq(DataBaseMeta::getDatabaseType, databaseType));
        if (Objects.nonNull(db)) {

            return Generate.generateDataBaseMeta(db.getDbType(), db.getName(),
                    db.getHost(), db.getPort(), db.getUserName(), db.getDbPassword(), db.getSchame());
        }
        return null;
    }
}
